package com.ruoyi.common.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date()); // 起始版本 3.3.0(推荐使用)
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "createBy", () -> SecurityUtils.getLoginUser().getUser().getUserId()!=null?SecurityUtils.getLoginUser().getUser().getUserId():-1L, Long.class);
        this.strictInsertFill(metaObject, "createUserName", () -> SecurityUtils.getLoginUser().getUsername()!=null?SecurityUtils.getLoginUser().getUsername():"-1", String.class);
        this.strictInsertFill(metaObject, "updateBy", () -> SecurityUtils.getLoginUser().getUser().getUserId()!=null?SecurityUtils.getLoginUser().getUser().getUserId():-1L, Long.class);
        this.strictInsertFill(metaObject, "updateUserName", () -> SecurityUtils.getLoginUser().getUsername()!=null?SecurityUtils.getLoginUser().getUsername():"-1", String.class);
        this.strictInsertFill(metaObject, "version", () -> 0, Integer.class);
        this.strictInsertFill(metaObject, "deleteFlag", () -> 0, Integer.class);
        this.strictInsertFill(metaObject, "auditorId", () -> SecurityUtils.getLoginUser().getUser().getUserId()!=null?SecurityUtils.getLoginUser().getUser().getUserId():-1L, Long.class);
        this.strictInsertFill(metaObject, "auditorName", () -> SecurityUtils.getLoginUser().getUsername()!=null?SecurityUtils.getLoginUser().getUsername():"-1", String.class);
        this.strictInsertFill(metaObject, "auditorTime", Date.class, new Date());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date()); // 起始版本 3.3.0(推荐)
        this.strictInsertFill(metaObject, "updateBy", () -> SecurityUtils.getLoginUser().getUser().getUserId()!=null?SecurityUtils.getLoginUser().getUser().getUserId():-1L, Long.class);
        this.strictInsertFill(metaObject, "updateUserName", () -> SecurityUtils.getLoginUser().getUsername()!=null?SecurityUtils.getLoginUser().getUsername():"-1", String.class);
        this.strictInsertFill(metaObject, "auditorId", () -> SecurityUtils.getLoginUser().getUser().getUserId()!=null?SecurityUtils.getLoginUser().getUser().getUserId():-1L, Long.class);
        this.strictInsertFill(metaObject, "auditorName", () -> SecurityUtils.getLoginUser().getUsername()!=null?SecurityUtils.getLoginUser().getUsername():"-1", String.class);
        this.strictInsertFill(metaObject, "auditorTime", Date.class, new Date());
    }

}